﻿using Newtonsoft.Json;
using Robin.Logging;
using Robin.Specifications;
using Robin.Web.Mvc.Controllers;
using Smart.Project.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Smart.Project.Web.Controllers
{
    public class ProjectinfoController : RobinController
    {
        IProjectinfoRepository _resp;
        IDepartmentRepository _depresp;
        public ProjectinfoController(IProjectinfoRepository resp, IDepartmentRepository depresp)
        {
            _resp = resp;
            _depresp = depresp;
        }
        // GET: Projectinfo
        public ActionResult Index()
        {
            return View();
        }
        // GET: Projectinfo
        public ActionResult DPIndex()
        {
            return View();
        }
        //Create
        public ActionResult Create()
        {
            MProjectinfo model = new MProjectinfo() { Id = System.Guid.NewGuid().ToString()};
            return View(model);
        }

        [HttpPost]
        public string Create(MProjectinfo model)
        {
            string result = "false";
            var dateNow = DateTime.Now;
            try
            {
                var pmodel = _resp.Count(a => a.NAME == model.NAME );
                if (pmodel >0)
                {
                    result = "项目名称已存在！";
                }
                else
                {
                    model.CREATETIME = decimal.Parse(dateNow.ToString("yyyyMMddHHmmss"));
                    model.UPDATETIME = decimal.Parse(dateNow.ToString("yyyyMMddHHmmss"));
                    model.DEPT = model.DEPT.Id != null ? _depresp.Get(model.DEPT.Id) : null;
                    _resp.Insert(model);
                    result = "true";
                }
            }
            catch (Exception ex)
            {
                result = "false";
                LogManager.Logger.Error("ProjectinfoController Create", ex);
            }
            return result;
        }

        //Updata
        public ActionResult Update(string Id)
        {
            MProjectinfo model = _resp.Get(Id);
            return View(model);
        }
        [HttpPost]
        public string GetProject(string Id)
        {
            string result = string.Empty;
            MProjectinfo model = _resp.Get(Id);
            result = JsonConvert.SerializeObject(model);
            return result;
        }
        [HttpPost]
        public string Update(MProjectinfo model)
        {
            string result = "false";
            var dateNow = DateTime.Now;
            try
            {
                var pmodel = _resp.Count(a => a.NAME == model.NAME && a.Id != model.Id);
                if (pmodel >0)
                {
                    result = "项目名称已存在！";
                }
                else
                {
                    model.UPDATETIME = decimal.Parse(dateNow.ToString("yyyyMMddHHmmss"));
                    model.DEPT = model.DEPT.Id != null ? _depresp.Get(model.DEPT.Id) : null;
                    _resp.Update(model);
                    result = "true";
                }
            }
            catch (Exception ex)
            {
                result = "false";
                LogManager.Logger.Error("ProjectinfoController Update", ex);
            }
            return result;
        }

        //Detail
        public ActionResult Detail(string Id)
        {
            MProjectinfo model = _resp.Get(Id);
            return View(model);
        }

        public string Delete(string id)
        {
            string result = "false";
            try
            {
                _resp.Delete(id);
                result = "true";
            }
            catch (Exception ex)
            {
                result = "false";
                LogManager.Logger.Error("ProjectinfoController Delete", ex);
            }
            return result;
        }
        public string GetAllTree()
        {
            string result = string.Empty;
            try {
                var deplist = _depresp.GetAllList();
                List<MadinTree> formlist = new List<MadinTree>();
                foreach (var deptree in deplist)
                {
                    MadinTree dm = new MadinTree();
                    List<MadinTree> pmlist = new List<MadinTree>();
                    dm.name = deptree.BMMC;
                    dm.id = deptree.Id;
                    var prolist = _resp.GetAllList(a=>a.DEPT.Id == dm.id);
                    foreach (var protree in prolist)
                    {
                        MadinTree pm = new MadinTree();
                        pm.childMenus = null;
                        pm.id = protree.Id;
                        pm.name = protree.NAME;
                        pmlist.Add(pm);
                    }
                    dm.childMenus = pmlist;
                    formlist.Add(dm);
                }
                result = JsonConvert.SerializeObject(formlist);
            }

            catch (Exception ex)
            {
                LogManager.Logger.Error("ProjectinfoController GetAllTree", ex);
            }
            return result;
        }
        public string GetProjectinfoList()
        {
            string result = "[]";
            string name = Request["NAME"];
            string echo = Request["sEcho"];
            string iDisplayStart = Request["iDisplayStart"];
            string iDisplayLength = Request["iDisplayLength"];
            int count = 0;
            int startIndex = 0, pagesize = 7;
            int.TryParse(iDisplayStart, out startIndex);
            int.TryParse(iDisplayLength, out pagesize);
            try
            {
                Expression<Func<MProjectinfo, bool>> where = item => true;
            FormatedList<List<MProjectinfo>> formlist = new FormatedList<List<MProjectinfo>>();
            where = where.And(item => item.NAME.Contains(name));
            var list = _resp.GetList(where, obj => obj.Asc(item => item.Id), startIndex, pagesize).ToList();

                for (int i = 0; i < list.Count; i++)
                {
                    list[i].NUMBER = i + 1;
                }
                count = _resp.GetAllList(where).Count();
                formlist.sEcho = int.Parse(echo);
                formlist.iTotalRecords = count;
                formlist.iTotalDisplayRecords = count;
                formlist.aaData = list.ToList();
                result = JsonConvert.SerializeObject(formlist);
            }
            catch (Exception ex)
            {
                LogManager.Logger.Error("ProjectinfoController GetProjectinfoList", ex);
            }
            return result;
        }
    }
}